#include "RoundFunctionNode.h"
#include "..\Common\AssertHelper.h"
#include "..\Common\MathHelper.h"

using namespace std;

namespace CExpression
{
	void RoundFunctionNode::Eval(ExecuteContext &context)
	{
		(this->parameters)[0]->Eval(context);
		if(AssertHelper::ExpectType(context.TempResultType(),ExecuteRunTimeType::Double,context,TEXT("Round function parameter must be double type.")))
		{
			double operand1=context.GlobalTempNum();
			(this->parameters)[1]->Eval(context);
			if(AssertHelper::ExpectType(context.TempResultType(),ExecuteRunTimeType::Double,context,TEXT("Round function parameter must be double type.")))
			{
				context.SetGlobalTempNum(MathHelper::Round(operand1,static_cast<int>(context.GlobalTempNum())));
				context.SetTempResultType(ExecuteRunTimeType::Double);
			}
		}
	}

	shared_ptr<Node> RoundFunctionNode::Clone(Expression &parent)
	{
		shared_ptr<Node> result(new RoundFunctionNode());
		RoundFunctionNode* p=static_cast<RoundFunctionNode*>(result.get());
		p->AddParameter((this->parameters)[0]->Clone(parent));
		p->AddParameter((this->parameters)[1]->Clone(parent));
		return result;
	}
}